/*jslint browser: true*/
/*global $, jQuery, alert, window*/

function is_major_booked(major_id) {
    "use strict";
    var booked_major_id_list = window.storage(window.booked_major_key);
    return window.is_in_array(major_id, booked_major_id_list);
}

function get_major_by_id(major_id) {
    "use strict";
    var major_list = window.format.s2c(window.storage(window.major_all_key),
                                       window.format.major),
        index,
        length = major_list.length,
        major_item;
    for (index = 0; index < length; index = index + 1) {
        major_item = major_list[index];
        if (major_item.id === major_id) {
            return major_item;
        }
    }
    return false;
}


window.view = {};
window.sync = {
    "mymajor_data_html": function () {
        "use strict";
        var booked_id_list = window.storage(window.booked_major_key),
            major_all = window.format.s2c(window.storage(window.major_all_key),
                                          window.format.major),
            index,
            length = booked_id_list.length,
            major_item,
            time,
            tr_length = 4,
            detail = [];
        for (index = 0; index < length; index = index + 1) {
            major_item = get_major_by_id(booked_id_list[index]);
            time = index % tr_length;
            if (time === 0) {
                detail.push("<table><tr>");
            }
            detail.push('<td class="item">');
            detail.push('<a href="/major/{major_id}/index/">'.format({
                major_id: major_item.id
            }));
            detail.push('<div></div><span>{major_name}</span>'.format({
                major_name: major_item.name
            }));
            detail.push('<div class="neg unbook">开始学习</div></a></td>');
            if (time !== 3) {
                detail.push('<td class="item_none"></td>');
            } else {
                detail.push('</tr></table><div class="space"></div>');
            }
        }
        $("#my_major").html("");
        $(detail.join("")).appendTo("#my_major");
    
    },
    "allmajor_data_html": function () {
        "use strict";
        var major_list = window.format.s2c(window.storage(window.major_all_key),
                                           window.format.major),
            index,
            length = major_list.length,
            major_item,
            time,
            is_booked,
            tr_length = 4,
            detail = [];
        for (index = 0; index < length; index = index + 1) {
            major_item = major_list[index];
            time = index % tr_length;
            is_booked = is_major_booked(major_item.id);
            if (time === 0) {
                detail.push("<table><tr>");
            }
            detail.push('<td class="item">');
            detail.push('<a book={is_booked} major_id={major_id} href="javascript:void(0)">'.format({
                is_booked: is_booked,
                major_id: major_item.id
            }));
            detail.push('<div></div><span>{major_name}</span>'.format({
                major_name: major_item.name
            }));
            if (is_booked) {
                detail.push('<div class="posi booked">已开通</div></a></td>');
            } else {
                detail.push('<div class="neg unbook">现在开通</div></a></td>');
            }
            if (time !== 3) {
                detail.push('<td class="item_none"></td>');
            } else {
                detail.push('</tr></table><div class="space"></div>');
            }
        }
        $("#all_major").html("");
        $(detail.join("")).appendTo("#all_major");
    }
};
window.init = {
    "global_var": function () {
        "use strict";
        window.major_all_key = "/major/";
        window.booked_major_key = "/user/major/";
        window.booking_key = "/user/major/{id}";
    },
    "html_major_detail": function () {
        "use strict";
        window.sync.mymajor_data_html();
        window.sync.allmajor_data_html();
    },
    "addEventListener": function () {
        "use strict";
        $(".index_major").delegate("a", "click",
                                   window.listener.book_major);
        window.hideLoading();
    }
};
window.init_func_list = ["global_var",
                         "html_major_detail",
                         "addEventListener"];
window.listener = {
    "book_major": function (event) {
        "use strict";
        var element = $(this),
            is_booked = element.attr("book") === "true",
            major_id = parseInt(element.attr("major_id"), 10),
            key = "/user/major/{major_id}/".format({
                major_id: major_id
            }),
            booked_key = window.booked_major_key,
            saved_list;
        if (is_booked) {
            return true;
        }
        window.loading();
        saved_list = window.storage(booked_key);
        if (window.is_in_array(major_id, saved_list)) {
            window.hideLoading();
            return true;
        }
        $.ajax({
            url:  key,
            type: "PUT",
            dataType: "json",
            async: true,
            success: function (result, status) {
                var booked_major_id = result.data[0].major_id,
                    booked_key = window.booked_major_key,
                    saved_list = window.storage(booked_key);
                saved_list.push(booked_major_id);
                window.storage(booked_key, saved_list);
                window.sync.mymajor_data_html();
                window.sync.allmajor_data_html();
                window.hideLoading();
            }
        });
        
    }
};